//
//  GroupSystemNet.m
//  SportSocial
//
//  Created by yull on 15/1/29.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "GroupSystemNet.h"
#import "ChatSystemInfo.h"
#import "ChatSysInfoDao.h"

@implementation GroupSystemNet

+ (instancetype)shareInstance
{
    static GroupSystemNet *systemNet = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        systemNet = [[GroupSystemNet alloc] init];
    });
    return systemNet;
}

- (instancetype)init
{
    if (self = [super init]) {
        NSArray *pidArray = @[[NSNumber numberWithInteger:Group_List_Receive_PID],
                              [NSNumber numberWithInteger:Group_Notify_Receive_PID]];
        [self.arrayPid addObjectsFromArray:pidArray];
    }
    return self;
}

//Ack私聊消息
- (void)sendGroupMsgAckWithMsgArray:(NSArray *)msgArray
{
    GroupNotifyCGMessage_Builder *builder = [GroupNotifyCGMessage builder];
    for (GroupNotifyMessage *message in msgArray) {
        builder = [builder addMsgIds:message.msgId];
    }
    
    GroupNotifyCGMessage *ackMsg = [builder build];
    NSData *data = [ackMsg data];
    
    [[IM_Service instance] sendData:data protocolId:Group_Notify_Send_PID];
}

#pragma mark - 收到消息
- (void)handlePacketWithPid:(NSInteger)pid recvData:(NSData *)recvData
{
    switch (pid) {
        case Group_List_Receive_PID:
            [self handleGroupListWithData:recvData];
            break;
        case Group_Notify_Receive_PID:
            [self handleNotifyReceive:recvData];
            break;
        default:
            break;
    }
}

- (void)handleGroupListWithData:(NSData *)recvData
{
    GroupListGCMessage *groupListGCMessage = [GroupListGCMessage parseFromData:recvData];
    
    for (GroupInfo *groupInfo in groupListGCMessage.groupInfoList) {
        
        if (![[CurrentUser shareInstance] isMyGroup:groupInfo.groupId] && groupInfo.state == 1) {
            [[CurrentUser shareInstance] addGroup:groupInfo];
        } else if ([[CurrentUser shareInstance] isMyGroup:groupInfo.groupId] && groupInfo.state == 2) {
            [[CurrentUser shareInstance] deleteGroup:groupInfo.groupId];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"group_is_deleted" object:nil userInfo:@{@"groupid":@(groupInfo.groupId)}];
        }
    }
}

- (void)handleNotifyReceive:(NSData *)recvData
{
    GroupNotifyGCMessage *notifyMessage = [GroupNotifyGCMessage parseFromData:recvData];
    NSMutableArray *chatInfoArray = [NSMutableArray array];
    for (GroupNotifyMessage *message in notifyMessage.messageListList) {
        [chatInfoArray addObject:[ChatSystemInfo systemInfowithGroupMsg:message]];
    }
    
    BOOL ret = [[ChatSysInfoDao shareInstance] addChatInfoArray:chatInfoArray];
    
    if (ret) {
        NSLog(@"群系统消息存储成功!");
        
        [SoundManager playSound];
        
        [self sendGroupMsgAckWithMsgArray:notifyMessage.messageListList];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ChatSystemInfoReload" object:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ChatInfoReload" object:nil];
    }

}

@end
